package jjn.offer2;

/**
 * @author Jiang Jining
 * @since 2023-07-02 11:37
 */
public class Offer2_12 {
    public int pivotIndex(int[] nums) {
        long sum = 0;
        for (int num : nums) {
            sum += num;
        }
        long half = 0;
        for (int i = 0; i < nums.length; i++) {
            half += nums[i];
            if (half * 2 == sum + nums[i]) {
                return i;
            }
        }
        return -1;
    }
    
    public static void main(String[] args) {
        int[] nums = new int[]{1, 7, 3, 6, 5, 6};
        int pivotIndex = new Offer2_12().pivotIndex(nums);
        System.out.println("pivotIndex = " + pivotIndex);
    }
}
